import { CatConfig } from "./configs/CatConfig";
import { EventData } from "./datas/EventData";
import { GameData } from "./datas/GameData";
import { GameCatAnimData, GameCatData } from "./GameDefine";
import { AnimMgr } from "./managers/AnimMgr";
import { EventMgr } from "./managers/EventMgr";
import { ObjectUtils } from "./utils/ObjectUtils";

/**
 * 游戏小猫
 * @Author: lzmxqh  
 * @Date: 2022-10-19 17:22:42  
 * @Last Modified by: lzmxqh
 * @Last Modified time: 2022-10-23 23:50:19
 */
export class GameCat extends Phaser.GameObjects.Container {
    /**小猫数据 */
    private catData: GameCatData;

    /**小猫模型 */
    public model: Phaser.GameObjects.Sprite;

    /**行走缓动 */
    private walkTween: Phaser.Tweens.Tween;

    constructor(scene: Phaser.Scene) {
        super(scene);
        const t = this;
        scene.add.existing(t);
        t.initUI();
    }

    /**获取小猫数据 */
    public getCatData(): GameCatData {
        return ObjectUtils.deepCopy(this.catData);
    }

    /**初始化UI */
    private initUI(): void {
        const t = this;
        const model: Phaser.GameObjects.Sprite = t.scene.add.sprite(0, 0, "").setOrigin(0.65, 0.5).setScale(1.2);
        t.add(model);

        model.on("animationrepeat", () => {
            this.updateState();

            t.catData = GameData.getCatPosByDir(t.catData);
            t.playWalkAnim(GameData.getBlockX(t.catData.row, t.catData.col), GameData.getBlockY(t.catData.row));
        });
        t.model = model;
    }

    /**刷新状态 */
    private updateState(): void {
        const t = this;
        t.model.anims.stop();
        t.walkTween && t.scene.tweens.remove(t.walkTween);

        const catData: GameCatData = t.catData;

        t.x = GameData.getBlockX(catData.row, catData.col);
        t.y = GameData.getBlockY(catData.row);

        const configData: GameCatAnimData = CatConfig.CAT_CONFIG[catData.dir];
        t.model.scaleX = configData.scaleX * 1.5;

        t.model.setOrigin(configData.originX, configData.originY);
        t.model.setTexture(CatConfig.getIdleCatTexture(catData.dir, true));
    }

    /**重置数据 */
    public reset(catData: GameCatData): void {
        const t = this;
        t.catData = catData;
        t.updateState();
    }

    /**
     * 前往地块
     * @param tRow      目标行
     * @param tCol      目标列
     * @param [isEnd]   是否结束游戏
     * @param [tDir]    跑路方向
     */
    public gotoBlock(tRow: number, tCol: number, isEnd?: boolean, tDir?: number): void {
        const t = this;
        const catData: GameCatData = t.catData;
        catData.dir = isEnd ? tDir : GameData.getCatDir(catData.row, catData.col, tRow, tCol);

        t.updateState();

        catData.row = tRow;
        catData.col = tCol;

        AnimMgr.getInst().playCatAnim(t, catData.dir, isEnd ? false : true);
        t.playWalkAnim(GameData.getBlockX(tRow, tCol), GameData.getBlockY(tRow));

        !isEnd && t.model.once("animationcomplete", () => {
            t.updateState();

            // 发送移动结束事件
            EventMgr.getInst().emit(EventData.MOVE_END);
        });
    }

    /**播放行走动画 */
    private playWalkAnim(x: number, y: number): void {
        const t = this;
        const targetX: number = t.x + (x - t.x) * 0.4;
        const targetY: number = t.y + (y - t.y) * 0.4;

        t.walkTween && t.scene.tweens.remove(t.walkTween);
        t.walkTween = t.scene.tweens.add({
            targets: t,
            props: {
                x: { value: targetX },
                y: { value: targetY }
            },
            duration: 300,
            ease: "Linear",
            onComplete: (tween: Phaser.Tweens.Tween, targets: any[], ...param: any[]) => {
                t.walkTween = null;
            },
        });
    }
}